package role

import (
	"sanguo/cluster"
	"sanguo/codec/cs"
	"sanguo/node/common/aoi"
	"sanguo/node/node_map/scene"
	cs_message "sanguo/protocol/cs/message"

	"github.com/golang/protobuf/proto"
)

var encoder *cs.Encoder

type RoleData struct {
	ID         uint64 //角色ID
	Lev        int32  //角色等级
	Name       string //角色名
	Portrait   int32  //头像
	Outline    int32  //外形
	Face       int32
	Hair       int32
	HairColor  int32
	EyeColor   int32
	Cloth      int32
	ClothColor int32
	Weapon1    int32
	Weapon2    int32
	StoryStage int64
}

type Role struct {
	aoi.AOIObjectBase
	RoleData
	UserID   string
	Scene    *scene.Scene
	ScenePos scene.Position
	LastPos  scene.Position
	Game     cluster.PeerID
	enter    map[*Role]bool
	leave    map[*Role]bool
}

func (this *Role) PackRoleData() *cs_message.ViewObj {
	return &cs_message.ViewObj{
		UserID:     proto.String(this.UserID),
		RoleID:     proto.Uint64(this.ID),
		Face:       proto.Int32(this.Face),
		Hair:       proto.Int32(this.Hair),
		HairColor:  proto.Int32(this.HairColor),
		EyeColor:   proto.Int32(this.EyeColor),
		Outline:    proto.Int32(this.Outline),
		Portrait:   proto.Int32(this.Portrait),
		Name:       proto.String(this.Name),
		Weapon1:    proto.Int32(this.Weapon1),
		Weapon2:    proto.Int32(this.Weapon2),
		Cloth:      proto.Int32(this.Cloth),
		ClothColor: proto.Int32(this.ClothColor),
		Level:      proto.Int32(this.Lev),
		Pos: &cs_message.Position{
			X: proto.Int32(this.ScenePos.X),
			Y: proto.Int32(this.ScenePos.Y),
			Z: proto.Int32(this.ScenePos.Z),
		},
		Angle: proto.Int32(this.ScenePos.Angle),
	}
}

func (this *Role) GetID() string {
	return this.UserID
}

func (this *Role) SetScene(scene_ *scene.Scene) {
	this.Scene = scene_
}

func (this *Role) GetScene() *scene.Scene {
	return this.Scene
}

func (this *Role) Tick() {
	this.SendEnterSee()
	this.SendLeaveSee()

	if (this.ScenePos.X != this.LastPos.X) ||
		(this.ScenePos.Y != this.LastPos.Y) ||
		(this.ScenePos.Z != this.LastPos.Z) {
		this.LastPos = this.ScenePos
		this.NotifyPosUpdate()
	}
}

func init() {
	encoder = cs.NewEncoder("sc")
}
